<script setup>
import { reactive, ref, watch } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
import { deleteZoneItem, queryZoneList } from '@/service/zone.js'
import ZoneManagementDialog from '@/pages/ZoneManagement/components/ZoneManagementDialog.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { deleteHotSaleRankingItem } from '@/service/hot-sales-ranking.js'
const totalRecords = ref(0)
const searchForm = reactive({
  categoryName: '',
  page: 1,
  pageSize: 10,
})
const categoryList = ref([])

async function loadData() {
  const res = await queryZoneList(searchForm.categoryName, searchForm.page, searchForm.pageSize)
  totalRecords.value = res.data.total
  categoryList.value = res.data.records
  console.log(categoryList.value)
}

watch(
  searchForm,
  () => {
    loadData()
  },
  { immediate: true, deep: true }
)

function clearQueryParam() {
  Object.assign(searchForm, {
    categoryName: '',
    page: 1,
    pageSize: 10,
  })
  loadData()
}
const zoneManagementDialogRef = ref()
const type = ref('add')
function addNewItem() {
  type.value = 'add'
  zoneManagementDialogRef.value.showModal = true
}
function onTapNavigateToCategoryPage() {
  router.push({ name: 'category' })
}
function onTapEditButton(row) {
  type.value = 'edit'
  zoneManagementDialogRef.value.form.categoryId = row.categoryId
  zoneManagementDialogRef.value.form.status = row.status
  zoneManagementDialogRef.value.showModal = true
}
function onTapCheckButton(row) {}
function onTapDeleteButton(row) {
  ElMessageBox.confirm(`确定删除首页条目：<span style="color: red">${row.categoryName}</span>?`, '信息', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
    dangerouslyUseHTMLString: true, // 允许内容使用 HTML 字符串
  })
    .then(async () => {
      await deleteZoneItem(row.categoryId)
      ElMessage({
        type: 'success',
        message: '成功删除该首页条目',
      })
      await loadData()
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}
</script>

<template>
  <div class="zone-management">
    <el-form :model="searchForm" inline label-position="right" label-width="auto" style="margin-top: 10px">
      <el-form-item label="分类名称:" style="width: 100%">
        <div style="display: flex; align-items: center; width: 100%">
          <el-input v-model="searchForm.categoryName" placeholder="请输入分类名称" style="width: 300px"></el-input>
          <el-button style="margin-left: 10px" type="info" @click="clearQueryParam">清空查询</el-button>
          <el-button style="margin-left: auto" type="primary" @click="addNewItem">添加条目</el-button>
          <el-button style="margin-left: 10px" type="info" @click="onTapNavigateToCategoryPage">分类管理界面</el-button>
        </div>
      </el-form-item>
    </el-form>
    <el-table
      :data="categoryList"
      :header-cell-style="() => ({ background: '#eef1f6', color: '#606266' })"
      border
      style="width: 100%"
    >
      <el-table-column align="center" label="分类ID">
        <template #default="scope">
          <span>{{ scope.row.categoryId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="分类名称">
        <template #default="scope">
          <span>{{ scope.row.categoryName }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否展示">
        <template #default="scope">
          <el-tag v-if="scope.row.status === 0" type="primary">展示中</el-tag>
          <el-tag v-if="scope.row.status === 1" type="danger">未展示</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间">
        <template #default="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="更新时间">
        <template #default="scope">
          <span>{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="300">
        <template #default="scope">
          <el-button type="primary" @click="onTapEditButton(scope.row)">编辑</el-button>
          <el-button type="default" @click="onTapCheckButton(scope.row)">查看</el-button>
          <el-button type="danger" @click="onTapDeleteButton(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="searchForm.page"
      v-model:page-size="searchForm.pageSize"
      :page-sizes="[10, 20, 30, 40]"
      :total="totalRecords"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top: 10px"
    />
  </div>
  <ZoneManagementDialog ref="zoneManagementDialogRef" :type="type" @update="loadData"></ZoneManagementDialog>
</template>

<style lang="scss" scoped>
.zone-management {
  background-color: #fff;
  padding: 20px;
}
</style>
